package com.prime.tutorial.moviestore.dao;

import com.prime.tutorial.moviestore.domain.Groups;
import com.prime.tutorial.moviestore.domain.Users;
import org.springframework.stereotype.Repository;
//import org.springframework.orm.jpa.support.JpaDaoSupport;

import javax.persistence.Query;
import java.util.List;


@Repository
//public class UserDAOWithJPA extends JpaDaoSupport implements UserDAO{
public class UserDAOWithJPA extends GenericDAOWithJPA<Users, Long> implements UserDAO {

    public List<Users> findByLastName(String lastname) {
        Query query = entityManager.createQuery("Select distinct m from Users m where m.lastname = ?1");
        query.setParameter(1, lastname);
        return query.getResultList();
//        return getJpaTemplate().find("Select m from Users m where m.lastname = ?1",lastname);
    }

    public List<Users> findByLogin(String login) {
        Query query = entityManager.createQuery("Select distinct m from Users m where m.login = ?1");
        query.setParameter(1, login);
        return query.getResultList();
//        return getJpaTemplate().find("Select m from Users m where m.login = ?1",login);
    }

    public List<Users> findEnabled() {
        Query query = entityManager.createQuery("Select distinct m from Users m where m.enabled = 1");
        return query.getResultList();
//     return getJpaTemplate().find("Select m from Users m where m.enabled = 1");
    }

    public List<Users> findByGroup(Groups group) {
        Query query = entityManager.createQuery("Select distinct u from Users u inner join u.group as g where g.name = ?1");
        query.setParameter(1, group.getName());
        return query.getResultList();
//        return getJpaTemplate().find("Select u from Users u inner join Group g where g.name = ?1",group.getName());
    }

       public List<Users> findOnlineUsers() {
           Query query = entityManager.createQuery("Select distinct u from UserActivity ua inner join ua.user as u where ua.logout = NULL");
           return query.getResultList();
    }
//    public Boolean checkIfUserCouldAddNewUsers(Users user) {
//        return null;  //To change body of implemented methods use File | Settings | File Templates.
//    }
}                                                                               